package com.atwl.school_system.controller;

import com.atwl.school_system.bean.Address;
import com.atwl.school_system.mapping.AddressDao;
import com.atwl.school_system.service.AddressService;
import com.atwl.school_system.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private AddressDao addressDao;

    @GetMapping("/address/get")
    public R getAddress() {
        return R.ok().setData(addressDao.getAddress());
    }

    @PostMapping("/address/add")
    public R addAddress(@RequestBody Address address) {
        addressService.addAddress(address);
        return R.ok();
    }

    @GetMapping("/address/getById")
    public R getAddressById(@RequestParam("id") Integer id) {
        return R.ok().setData(addressDao.getAddressById(id));
    }

    @GetMapping("/address/updateDefault")
    public R updateDefaultAddress(@RequestParam("id") Integer id, @RequestParam("flag") Integer flag) {
        addressDao.updateDefaultAddress(id, flag);
        addressDao.updateOther(id);
        return R.ok();
    }
}
